//
//  TimelineViewModel.m
//  RACTest
//
//  Created by Apple on 2017/12/6.
//  Copyright © 2017年 LYKM. All rights reserved.
//

#import "TimelineViewModel.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "StatusViewModel.h"
#import "Status.h"

@interface TimelineViewModel ()

/**   */
@property (nonatomic,strong) RACCommand *loadDataCommand;

@end

@implementation TimelineViewModel

- (RACSignal *)_fetchRemoteDataWithPara:(id)para {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        NSString *API = @"https://api.weibo.com/2/statuses/home_timeline.json";
        
        NSDictionary *parament = @{@"access_token" : @"2.00IJI6XDyY87OC09185c3ae5l57M4D" , @"page" : para};
        
        @weakify(self)
        
        [manager GET:API parameters:parament progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            @strongify(self)
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            self.statusesTitle = @"statuses";
            NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:1];
            for (NSDictionary *data in dic[@"statuses"]) {
                StatusViewModel*model = [[StatusViewModel alloc] initWithData:data];
                [temp addObject:model];
            }
            self.statuesArr = [NSArray arrayWithArray:temp];
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [subscriber sendCompleted];
            
        }];
        
        return nil;
    }];
}
- (RACSubject *)cellClickSubject {
    if (!_cellClickSubject) {
        _cellClickSubject = [RACSubject subject];
    }
    return _cellClickSubject;
}

@end
